package org.training.apr.model;

import java.util.ArrayList;
import java.util.List;

import org.training.apr.model.visitor.Visitor;

public class Enterprise extends Model {
	private String name;
	private Enterprise parent;
	private List<Enterprise> childs;
	
	public Enterprise() {
		this("");
	}
	
	public Enterprise(String name) {
		this.name = name;
		this.parent = null;
		this.childs = new ArrayList<Enterprise>();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<Enterprise> getChilds() {
		return childs;
	}
	
	public void setChilds(List<Enterprise> childs) {
		for(Enterprise e : childs) {
			e.setParent(this);
		}
		this.childs = childs;
	}

	public Enterprise getParent() {
		return parent;
	}

	private void setParent(Enterprise parent) {
		this.parent = parent;
	}
	
	public void addChild(Enterprise e) {
		childs.add(e);
		e.setParent(this);
	}
	
	public void removeChild(Enterprise e) {
		childs.remove(e);
		e.setParent(null);
	}
	
	public boolean isParent() {
		return childs.size() > 0;
	}
	
	public boolean isChild() {
		return parent != null;
	}
	
	public String toString() {
		return getName() + " [id:" + getId() + "]";
	}

	public void accept(Visitor visitor) {
		visitor.visit(this);
	}

	public Enterprise getElder() {
		Enterprise elder = this;
		while(elder.getParent() != null) {
			elder = elder.getParent();
		}
		return elder;
	}
}
